package com.yuou.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuou.annotation.AccessLimit;
import com.yuou.annotation.SystemLog;
import com.yuou.common.utils.Tree;
import com.yuou.controller.BaseExceptionController;
import com.yuou.model.Dept;
import com.yuou.service.DeptService;
import com.yuou.util.ConstantUtil;

@Controller
@RequestMapping(value = "/admin")
public class DeptControllerAdmin extends BaseExceptionController {
	@Autowired
    private DeptService deptService;
	
	
	/**
	  * 新增菜单功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 //@RequiresPermissions("sys:dept:add")
	 @RequestMapping(value = "/addDept",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.DEPT_ADD,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> addDept(String prarm,Dept dept,ServletRequest srequest) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 dept.setGmtCreate(new Date());
		 if(deptService.insertDept(dept)>0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 
	 
	 /**
	  * 按菜单id查询信息
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:dept:dept")
	 @RequestMapping(value = "/selectDeptById",method = RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> selectMenuById(Integer id,Integer parentId) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 Dept dept=deptService.selectById(id);
		 if(parentId==0) {
			 dept.setParentName("总部门");
		 }else {
			 dept.setParentName(deptService.selectNameByid(parentId));
		 }
		 if(dept!=null){
			 map.put("status", 200);
		 }
		 map.put("dept", dept);
		 return map;
	 }
	 
	 /**
	  * 更新菜单功能
	  * @param menu
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:dept:edit")
	 @RequestMapping(value = "/updateDept",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.DEPT_MODIFY,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateDept(String prarm,Dept dept) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 try {
		 if(deptService.updateByPrimaryKey(dept)>0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 }catch(Exception e) {
			 e.printStackTrace();
		 }
		 return map;
	 }
	 
	 /**
	  * 删除菜单功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 //@RequiresPermissions("sys:dept:remove")
	 @RequestMapping(value = "/deleteDept",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.DEPT_DELETE,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> deleteMenu(String prarm,Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(deptService.selectCountByid(id)>0) {
			 map.put("status", 202);
		 }else {
			 if(deptService.deleteByPrimaryKey(id)>0){
				 map.put("status", 200);
			 }else{
				 //0表示：删除失败
				 map.put("status", 0);
			 }
		 }
		 return map;
	 }
	 
	 /**
		 * 模糊组合分页查询角色信息(and)
		 * @param bloger
		 * @param page
		 * @param pageSize
		 * @return
		 * @throws Exception
		 */
		 @RequiresPermissions("sys:dept:dept")
		 @RequestMapping(value = "/selectAllDept")
		 @ResponseBody
		 @AccessLimit(seconds=1,maxCount=15)
		 /*@RequestParam(value="pageSize", required=true,defaultValue="100") Integer pageSize*/
		 public List<Dept> selectAllblogerWithRoles(Dept dept,@RequestParam(value="page", required=true,defaultValue="1") Integer page) throws Exception{
			 Map<String, Object> map=new HashMap<String, Object>();
			 if(dept.getName()!=null&&dept.getName()!=""){
				 map.put("name", dept.getName());
			 }
				 
			 List<Dept> deptList=deptService.selectAll(map);
			 Map<String, Object> returnMap=new HashMap<String, Object>();
			 if(deptList.size()>0){
				 returnMap.put("status", 200);
			 }else{
				 //500表示：返回值为Null
				 returnMap.put("status", 500);
			 }
			 
			 returnMap.put("menuList", deptList);
			 return deptList;
		 }
		 /**
		 * 模糊组合分页查询角色信息(and)
		 * @param bloger
		 */
		 @RequiresPermissions("sys:dept:dept")
		 @RequestMapping(value = "/deptTree")
		 @ResponseBody
		 public Tree<Dept> deptTree() throws Exception{
			 Tree<Dept> tree = new Tree<Dept>();
		        tree = deptService.getTree();
		        return tree;
		 }
}
